using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerHealth : Singleton<PlayerHealth>
{
    private Animator animator;
    [SerializeField] private AudioClip playerHurt;
    [SerializeField] private AudioSource audioSource;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    public void RefreshPlayerHealth(int value)
    {
        GameManager.GetInstance().SetHp = GameManager.GetInstance().GetHp + value;
    }

    public void RefreshPlayerStrength(int value)
    {
        GameManager.GetInstance().SetStrength = value;
    }

    public void TakeDamage(int damage)
    {
        GameManager.GetInstance().SetHp = GameManager.GetInstance().GetHp - damage;
        if(GameManager.GetInstance().GetHp <= 0)
        {
            GameManager.GetInstance().SetHp = 0;
        }
        checkDetectDeath();
    }

    private void checkDetectDeath()
    {
        if(GameManager.GetInstance().GetHp > 0)
        {
            audioSource.PlayOneShot(playerHurt);
            animator.SetBool("Hurting", true);
            animator.SetTrigger("Hurt");
        }
        else
        {
            animator.SetBool("Dying", true);
        }
    }

    public void SetPlayerHurtingEnd()
    {
        animator.SetBool("Hurting", false);
    }

}
